{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# **How to use HugChat - the unofficial Hugging Chat API**"
      ],
      "metadata": {
        "id": "R4FiZs77vKXr"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## **Install prerequisite libraries**"
      ],
      "metadata": {
        "id": "6jmfeB1PvL8_"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "g9bTbqH96XS2",
        "outputId": "452331b1-e565-4228-f241-9e80cfb956f6"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Collecting hugchat==0.1.0\n",
            "  Downloading hugchat-0.1.0-py3-none-any.whl (24 kB)\n",
            "Collecting python-dotenv\n",
            "  Downloading python_dotenv-1.0.0-py3-none-any.whl (19 kB)\n",
            "Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from hugchat==0.1.0) (2.27.1)\n",
            "Collecting requests-toolbelt (from hugchat==0.1.0)\n",
            "  Downloading requests_toolbelt-1.0.0-py2.py3-none-any.whl (54 kB)\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m54.5/54.5 kB\u001b[0m \u001b[31m3.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hRequirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->hugchat==0.1.0) (1.26.16)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->hugchat==0.1.0) (2023.7.22)\n",
            "Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.10/dist-packages (from requests->hugchat==0.1.0) (2.0.12)\n",
            "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->hugchat==0.1.0) (3.4)\n",
            "Installing collected packages: python-dotenv, requests-toolbelt, hugchat\n",
            "Successfully installed hugchat-0.1.0 python-dotenv-1.0.0 requests-toolbelt-1.0.0\n"
          ]
        }
      ],
      "source": [
        "! pip install hugchat==0.1.0 python-dotenv"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## **Load Hugging Face credentials**"
      ],
      "metadata": {
        "id": "tXioNOYMv1ti"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from dotenv import dotenv_values\n",
        "\n",
        "secrets = dotenv_values('hf.env')"
      ],
      "metadata": {
        "id": "tdJjllXGueGX"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "hf_email = secrets['EMAIL']\n",
        "hf_pass = secrets['PASS']"
      ],
      "metadata": {
        "id": "tNHBfLF788f2"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## **LLM response generation**"
      ],
      "metadata": {
        "id": "_yc18ezBzML6"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from hugchat import hugchat\n",
        "from hugchat.login import Login"
      ],
      "metadata": {
        "id": "H21niuMc8xcv"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Function for generating LLM response\n",
        "def generate_response(prompt_input, email, passwd):\n",
        "    # Hugging Face Login\n",
        "    sign = Login(email, passwd)\n",
        "    cookies = sign.login()\n",
        "    # Create ChatBot\n",
        "    chatbot = hugchat.ChatBot(cookies=cookies.get_dict())\n",
        "    return chatbot.chat(prompt_input)"
      ],
      "metadata": {
        "id": "4k9iUzyWzwSh"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "prompt = \"What is Streamlit?\"\n",
        "response = generate_response(prompt, hf_email, hf_pass)"
      ],
      "metadata": {
        "id": "TD0YIqY1zQmK"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "response"
      ],
      "metadata": {
        "id": "2Om92sQ4z3e2",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 125
        },
        "outputId": "89e9b29a-3a54-4e61-dd13-b75395fa82b6"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'Streamlit is a lightweight Python library for creating interactive data analysis tools such as visualizations and reports. It makes use of modern web technologies like React, Redux, and WebSockets to provide fast, responsive UI components that can be used to build custom user interfaces. At its core, Streamlit provides a set of high-level abstractions built on top of NumPy and Pandas that make it easy to create beautiful charts, tables, maps, and other types of output. It works seamlessly with popular libraries like Altair, Bokeh, Matplotlib, and Seaborn, allowing you to combine their unique strengths into powerful analytical tools. Overall, Streamlit simplifies the process of building data science applications by providing a unified interface that integrates development environments with deployment tools, making it possible to iterate quickly and easily share results.'"
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 11
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "aIWbxGM-1LDh"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}